﻿using Rw.Core.Common.Paging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WH.Contracts.RemoveWatermark;
using WH.Model.OrgContext;
using WH.Model.RemoveWatermark;
using WH.Repositories.BaseRepositories;
using WH.Repositories.IRepositories.RemoveWatermark;

namespace WH.Repositories.Repositories.RemoveWatermark
{
    public class RemoveWatermarkRepository : BaseRepository<RemoveWatermarkEntity>, IRemoveWatermarkRepository
    {
        private readonly ISqlSugarClient _dbContext;
        public RemoveWatermarkRepository(ISqlSugarClient dbContext, OrganizationContext organizationContext) : base(dbContext, organizationContext)
        { _dbContext = dbContext; }

        protected override ISugarQueryable<RemoveWatermarkEntity> Query
        {
            get
            {
                return base.Query
               .Includes(b => b.Organization);
            }
        }
        /// <summary>
        /// 分页检索
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<IPagedList<RemoveWatermarkOutputDto>> GetRemoveWatermarkByPageAsync(RemoveWatermarkFilterDto model)
        {
            var exp = Expressionable.Create<RemoveWatermarkEntity>();

            if (!string.IsNullOrWhiteSpace(model.Code))
            {
                exp.And(x => x.Code.Contains(model.Code));
            }
            if (!string.IsNullOrEmpty(model.KeyWord))
            {
                exp.And(x => x.Code.Contains(model.KeyWord) || x.PreUrl.Contains(model.KeyWord) || x.AfterUrl.Contains(model.KeyWord));
            }
            if (!string.IsNullOrEmpty(model.StartEndTime))
            {
                var timeArr = model.StartEndTime.Split(',').Select(x =>
                {
                    DateTime.TryParse(x, out var time);
                    return time;
                }).ToArray();
                if (timeArr.Length >= 2)
                {
                    exp.And(b => SqlFunc.Between(b.CreatedTime, timeArr[0], timeArr[1]));
                }
            }
            var data = await base.PagedFindAllAsync(exp.ToExpression(), model);
            var list = new PagedList<RemoveWatermarkOutputDto>()
            {
                PageIndex = data.PageIndex,
                PageSize = data.PageSize,
                TotalCount = data.TotalCount,
                Entities = data.Entities.Select((a) =>
                {
                    var item = new RemoveWatermarkOutputDto()
                    {
                        Id = a.Id,
                        Code = a.Code,
                        OpenId = a.OpenId,
                        PreUrl = a.PreUrl,
                        AfterUrl = a.AfterUrl,
                        Remark = a.Remark,
                        CreatedBy = a.CreatedBy,
                        CreatedTime = a.CreatedTime,
                        UpdatedBy = a.UpdatedBy,
                        UpdatedTime = a.UpdatedTime,
                    };
                    return item;
                })
            };
            return list;
        }
        /// <summary>
        /// 数据导出
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public async Task<IEnumerable<RemoveWatermarkExportDto>> DataExportAsync(RemoveWatermarkFilterDto model)
        {
            var exp = Expressionable.Create<RemoveWatermarkEntity>();

            if (!string.IsNullOrWhiteSpace(model.Code))
            {
                exp.And(x => x.Code.Contains(model.Code));
            }
            if (!string.IsNullOrEmpty(model.KeyWord))
            {
                exp.And(x => x.Code.Contains(model.KeyWord) || x.PreUrl.Contains(model.KeyWord) || x.AfterUrl.Contains(model.KeyWord));
            }
            if (!string.IsNullOrEmpty(model.StartEndTime))
            {
                var timeArr = model.StartEndTime.Split(',').Select(x =>
                {
                    DateTime.TryParse(x, out var time);
                    return time;
                }).ToArray();
                if (timeArr.Length >= 2)
                {
                    exp.And(b => SqlFunc.Between(b.CreatedTime, timeArr[0], timeArr[1]));
                }
            }
            var data = await base.FindAllAsync(exp.ToExpression());

            var list = data.Select((a) =>
            {
                var item = new RemoveWatermarkExportDto()
                {
                    OpenId = a.OpenId,
                    PreUrl = a.PreUrl,
                    AfterUrl = a.AfterUrl,
                    CreatedTime = a.CreatedTime,
                    Remark = a.Remark,
                };
                return item;
            });
            return list;
        }
    }
}

